package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.wemedia.WmUserControllerApi;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    WmUserService wmUserService;

    /**
     * 保存自媒体用户
     * @param wmUser
     * @return wmUser 需要主键返回
     */
    @Override
    @PostMapping("/save")
    public WmUser save(@RequestBody WmUser wmUser) {
        if(wmUserService.save(wmUser)){
            return wmUser;
        }
        return null;
    }

    /**
     * 根据登录名查询当前用户
     * @param name
     * @return WmUser
     */
    @Override
    @GetMapping("/findByName/{name}")
    public WmUser findByName(@PathVariable("name") String name) {
        WmUser wmUser = wmUserService.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return wmUser;
    }

    /**
     * 根据id查询自媒体用户
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmUser findWmUserById(@PathVariable("id") Integer id) {
        return wmUserService.getById(id);
    }
}
